package com.yupi.yuoj.controller;

import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private UserService userService;

    /**
     * 测试Redis序列化
     */
    @GetMapping("/redis")
    public BaseResponse<String> testRedis(HttpServletRequest request) {
        try {
            // 获取当前登录用户，这会触发Redis序列化
            User loginUser = userService.getLoginUser(request);
            if (loginUser != null) {
                return ResultUtils.success("Redis序列化测试成功，用户ID: " + loginUser.getId());
            } else {
                return ResultUtils.success("Redis序列化测试成功，但用户未登录");
            }
        } catch (Exception e) {
            return ResultUtils.error(500, "Redis序列化测试失败: " + e.getMessage());
        }
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public BaseResponse<String> health() {
        return ResultUtils.success("应用运行正常");
    }
}